package com.ruoyi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.common.util.WithdrawalOfSurplus;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.configuration.domain.MgTeamSigningRewards;
import com.ruoyi.configuration.service.IMgTeamSigningRewardsService;
import com.ruoyi.pay.manager.service.IPayManagerService;
import com.ruoyi.task.ParsePrice;
import com.ruoyi.team.service.ITeamInformationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 团队签约奖励Controller
 * 
 * @author ruoyi
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/configuration/teamsigningrewards")
public class MgTeamSigningRewardsController extends BaseController
{
    @Autowired
    private IMgTeamSigningRewardsService mgTeamSigningRewardsService;
    
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    
    private ITeamInformationService teamInformationService;
    
    private IPayManagerService payManagerService;
    
    RedisTemplate<String, Object> redisTemplate;
    
    
    
    @Resource
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Resource
    public void setTeamInformationService(ITeamInformationService teamInformationService) {
		this.teamInformationService = teamInformationService;
	}

    @Resource
	public void setPayManagerService(IPayManagerService payManagerService) {
		this.payManagerService = payManagerService;
	}


	@Autowired
    public void setThreadPoolTaskExecutor(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
    	this.threadPoolTaskExecutor = threadPoolTaskExecutor;
    }
    

    /**
     * 查询团队签约奖励列表
     */
    @PreAuthorize("@ss.hasPermi('configuration:teamsigningrewards:list')")
    @GetMapping("/list")
    public TableDataInfo list(MgTeamSigningRewards mgTeamSigningRewards)
    {
        startPage();
        List<MgTeamSigningRewards> list = mgTeamSigningRewardsService.selectMgTeamSigningRewardsList(mgTeamSigningRewards);
        return getDataTable(list);
    }

    /**
     * 导出团队签约奖励列表
     */
    @PreAuthorize("@ss.hasPermi('configuration:teamsigningrewards:export')")
    @Log(title = "团队签约奖励", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MgTeamSigningRewards mgTeamSigningRewards)
    {
        List<MgTeamSigningRewards> list = mgTeamSigningRewardsService.selectMgTeamSigningRewardsList(mgTeamSigningRewards);
        ExcelUtil<MgTeamSigningRewards> util = new ExcelUtil<MgTeamSigningRewards>(MgTeamSigningRewards.class);
        util.exportExcel(response, list, "团队签约奖励数据");
    }

    /**
     * 获取团队签约奖励详细信息
     */
    @PreAuthorize("@ss.hasPermi('configuration:teamsigningrewards:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(mgTeamSigningRewardsService.selectMgTeamSigningRewardsById(id));
    }

    /**
     * 新增团队签约奖励
     */
    @PreAuthorize("@ss.hasPermi('configuration:teamsigningrewards:add')")
    @Log(title = "团队签约奖励", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated MgTeamSigningRewards mgTeamSigningRewards)
    {
        int insertMgTeamSigningRewards = mgTeamSigningRewardsService.insertMgTeamSigningRewards(mgTeamSigningRewards);
		
        updateDiscount();
        
        return toAjax(insertMgTeamSigningRewards);
    }

    /**
     * 修改团队签约奖励
     */
    @PreAuthorize("@ss.hasPermi('configuration:teamsigningrewards:edit')")
    @Log(title = "团队签约奖励", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated MgTeamSigningRewards mgTeamSigningRewards)
    {
    	
        int updateMgTeamSigningRewards = mgTeamSigningRewardsService.updateMgTeamSigningRewards(mgTeamSigningRewards);
        
        
        updateDiscount();
        
        
		return toAjax(updateMgTeamSigningRewards);
    }

	protected void updateDiscount() {
		int count = teamInformationService.count();
        
        long maxId = teamInformationService.maxId();
        
        
        // 每次 读取1000 
        
        long ofSurplus = WithdrawalOfSurplus.ofSurplus(count, 500);
        
        
        ParsePrice task;
        
        List<MgTeamSigningRewards> selectList = this.mgTeamSigningRewardsService.selectMgTeamSigningRewardsList(new MgTeamSigningRewards());
        
        // 10000	1000条
        
        // 0
        
        ParsePrice.nextId = null;
        
        for (int i = 0; i < ofSurplus; i++) {
			
        	
        	task = new ParsePrice(teamInformationService, payManagerService, redisTemplate, selectList);
        	
        	task.setStart(maxId);
        	
        	task.setNum(500);
        	
			threadPoolTaskExecutor.execute(task);
		}
	}

    /**
     * 删除团队签约奖励
     */
    @PreAuthorize("@ss.hasPermi('configuration:teamsigningrewards:remove')")
    @Log(title = "团队签约奖励", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        int deleteMgTeamSigningRewardsByIds = mgTeamSigningRewardsService.deleteMgTeamSigningRewardsByIds(ids);
		
        updateDiscount();
        
        return toAjax(deleteMgTeamSigningRewardsByIds);
    }
}
